package com.jiyun.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Goods;
import com.jiyun.service.IBrandService;
import com.jiyun.service.ICategoryService;
import com.jiyun.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-06-06
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;
    @Autowired
    IBrandService brandService;
    @Autowired
    ICategoryService categoryService;

    @RequestMapping("zhu")
    public Map zhu(){
        return goodsService.zhu();
    }
    @RequestMapping("bing")
    public List<Map> bing(){
        return goodsService.bing();
    }


    @RequestMapping("upload")
    public void upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Goods.class, new ReadListener<Goods>() {
                    @Override
                    public void invoke(Goods goods, AnalysisContext analysisContext) {
                        Integer brandId = brandService.selectByName(goods.getBrandName());
                        Integer categoryId = categoryService.selectByName(goods.getCategoryName());
                        goods.setBrandId(brandId);
                        goods.setCategoryId(categoryId);
                        addGoods(goods);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }
                }).excelType(ExcelTypeEnum.XLSX)
                .sheet("物资列表").doRead();
    }


    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<Goods> list = goodsService.findAllName();

        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("物资列表.xlsx", "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");

        // easyExcel的导出
        EasyExcel.write(response.getOutputStream())
                .head(Goods.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("物资列表")
                .doWrite(list);

    }

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addGoods(@RequestBody Goods goods) {
        goodsService.save(goods);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateGoods(@RequestBody Goods goods) {
        goodsService.updateById(goods);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id) {
        goodsService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids) {
        goodsService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Goods findById(@RequestParam Integer id) {
        return goodsService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Goods> findAll() {
        return goodsService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Goods> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Goods goods) {
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        return goodsService.findPage(goodsPage, goods);
    }

}
